package com.infodms.dms.dao.sales.ordermanage.orderreport;

import java.sql.ResultSet;
import java.util.Map;

import org.apache.log4j.Logger;

import com.infodms.dms.common.Constant;
import com.infodms.dms.dao.common.BaseDao;
import com.infodms.dms.util.CommonUtils;
import com.infoservice.po3.bean.PO;
import com.infoservice.po3.bean.PageResult;

public class SpecialNeedOrderReoprtDAO extends BaseDao{
	public static Logger logger = Logger.getLogger(SpecialNeedOrderReoprtDAO.class);
	private static final SpecialNeedOrderReoprtDAO dao = new SpecialNeedOrderReoprtDAO ();
	public static final SpecialNeedOrderReoprtDAO getInstance() {
		return dao;
	}
	
	/**
	 * 查询可提报的订做车需求列表
	 * */
	public static PageResult <Map<String,Object>> queryCanReportSpecialList (String dealerId, String startDate,String endDate, int pageSize,int curPage){
		StringBuffer sql = new StringBuffer("");
		sql.append("SELECT TVSR.REQ_ID,\n");
		sql.append("       TMBA.AREA_NAME, --业务范围\n");  
		sql.append("	   TMBA.AREA_ID,\n");
		sql.append("       TMD.DEALER_CODE,\n");  
		sql.append("       TMD.DEALER_ID,\n");
		sql.append("       TMD.DEALER_SHORTNAME DEALER_NAME,\n");  
		sql.append("       TO_CHAR(TVSR.REQ_DATE, 'YYYY-MM-DD') REQ_DATE, --需求提报日期\n");  
		sql.append("       TO_CHAR(TVSR.REQ_CONFIRM_DATE, 'YYYY-MM-DD') REQ_CONFIRM_DATE, --需求确认日期\n");  
		sql.append("       TVSR.REFIT_DESC, --订做车改装说明\n");  
		sql.append("       TVSR.REQ_STATUS,\n");  
		sql.append("       SUM(TVSRD.AMOUNT) AMOUNT --提报数量\n");  
		sql.append("  FROM TT_VS_SPECIAL_REQ     TVSR,\n");  
		sql.append("       TT_VS_SPECIAL_REQ_DTL TVSRD,\n");  
		sql.append("       TM_DEALER             TMD,\n");  
		sql.append("       TM_BUSINESS_AREA      TMBA\n");  
		sql.append(" WHERE TVSR.REQ_ID = TVSRD.REQ_ID\n");  
		sql.append("   AND TVSR.DEALER_ID = TMD.DEALER_ID\n");  
		sql.append("   AND TVSR.AREA_ID = TMBA.AREA_ID\n");  
		sql.append("   AND TVSR.REQ_STATUS = "+Constant.SPECIAL_NEED_STATUS_08+"\n"); 
		
		if(!CommonUtils.isNullString(dealerId)) {
			sql.append("   AND TVSR.DEALER_ID IN (").append(dealerId).append(")\n") ;
		}
		
		if (null != startDate && !"".equals(startDate)) {
			sql.append("   AND TVSR.REQ_DATE >=\n");  
			sql.append("       TO_DATE('"+startDate+"', 'YYYY-MM-DD HH24:MI:SS')\n"); 
		}
		if (null != endDate && !"".equals(endDate)) {
			sql.append("   AND TVSR.REQ_DATE <=\n");  
			sql.append("       TO_DATE('"+endDate+"', 'YYYY-MM-DD HH24:MI:SS')\n");  
		} 
		
		sql.append("GROUP BY TVSR.REQ_ID,\n");
		sql.append("         TMBA.AREA_NAME,\n");  
		sql.append("         TMD.DEALER_CODE,\n");  
		sql.append("         TMD.DEALER_SHORTNAME,\n");  
		sql.append("         TVSR.REQ_DATE,\n");  
		sql.append("         TVSR.REQ_CONFIRM_DATE,\n");  
		sql.append("         TVSR.REFIT_DESC,\n");  
		sql.append("         TVSR.REQ_STATUS,\n");  
		sql.append("         TMBA.AREA_ID,\n");  
		sql.append("         TMD.DEALER_ID\n");

		sql.append(" ORDER BY TVSR.REQ_DATE DESC\n");

		return dao.pageQuery(sql.toString(), null,dao.getFunName(),pageSize ,curPage );
	}

	@Override
	protected PO wrapperPO(ResultSet rs, int idx) {
		// TODO Auto-generated method stub
		return null;
	}
	
}
